<?php
	$writeHead = "<?php
	
require_once(\"database.php\");
$%id = $%_GET['id'];
$%foreignKey = $%_GET['foreignKey'];
$%bodyString = \"\";
if ($%id) {
	$%SQLreadQuery = \"SELECT * FROM $tableName WHERE id = $%id\";
	$%SQLreadResult = $%db->query($%SQLreadQuery);
	while ($%rowArray = $%db->fetch_array($%SQLreadResult)) {\n";
	$writeBody = "".
"	}						
}

require_once('io_library.php');
if (session_id() != \"\") {
       //session_start();
       //echo \"starting a new session now\";
}
session_start();
if ($%_GET['sessionDepth']) { // if it needs a ref URL and they have SERVER data
       $%sessionDepth =  'depth' . $%_GET['sessionDepth'];
       if (!$%_SESSION[$%sessionDepth] || $%_SESSION[$%sessionDepth] == \"\") {
               //session_register($%sessionDepth);
               $%_SESSION[$%sessionDepth] = $%_SERVER['HTTP_REFERER'];
       }
       $%referringPage = $%_SESSION[$%sessionDepth]; // then give 'em what DEY need
}
else { session_unset(); }
//print_r($%_SESSION);

if (!$%referringPage)                                           // else send to parent 'list' page
       $%referringPage = \"".$tableName."_list.php\";
?>
<html>
	<head>
		<title>$siteName &gt; " .$displayNameArray[$tableName]. "</title>
		<?php require_once('io_writePage_css.php'); ?>
		<!-- Start JS Includes -->
		<?php require_once('io_writePage_js.php'); ?>
		<script type='text/javascript'>
			var refererURL = '<?php echo $%referringPage; ?>';
			var parent_id = '<?php echo $%id; ?>';
			var parent_table = '$tableName';
		</script>
	</head>
	<body class='writePage'>";
	
	include('navigation.php');
	
	$writeBody .= "
	<div id=\"fieldsList\">
		<form id='$tableName"."Form' method=\"post\" action=\"<?php echo "."$"."referringPage; ?>\"><?php ";
	$writeFooter = " ?>
		<input type=\"hidden\" name=\"id\" value=\"<?php echo $%_GET['id'];?>\" />
		<?php 
		if ($"."_GET['linkedChild']) {
			echo \"<input type='hidden' name='linkedChild' value='\".\$"."_GET['linkedChild'].\"' />\";
		}
		?>
		<input type=\"hidden\" name=\"TABLENAME\" value=\"$tableName\" />
		<div class='clearBoth'>&nbsp;</div>
		<input type=\"submit\" value=\"Save\" id='saveRecord' />
		</form>
		<input type=\"submit\" value=\"Cancel\" id='cancelRecord' />
	</div>
	</body>
</html>";
	$pageSource = str_replace("%", "", $writeHead) . str_replace("%", "", $fieldVarsString) . str_replace("%", "", $writeBody) . $inputElementsString . str_replace("%", "", $writeFooter);
	//echo str_replace("%", "", $pageSource);

?>